package com.example.dell.library.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.dell.library.R;
import com.example.dell.library.bean.Book;
import com.example.dell.library.bean.BorrorBook;
import com.example.dell.library.bean.BorrorBookList;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class BookBorrorAdapter extends RecyclerView.Adapter<BookBorrorAdapter.ViewHolder> {
    private List<BorrorBookList> blist;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvBname;
        TextView tvBbtime;
        TextView tvBdtime;
        Button btnBreturn;
        View bookView;
        public ViewHolder(View itemView) {
            super(itemView);
            bookView = itemView;
            tvBname = itemView.findViewById(R.id.tvbbname);
            tvBbtime = itemView.findViewById(R.id.tvbbtime);
            tvBdtime = itemView.findViewById(R.id.tvbdtime);
            btnBreturn = itemView.findViewById(R.id.btnbbreturn);
        }
    }
    public BookBorrorAdapter(List<BorrorBookList> blist) {
        this.blist = blist;
    }

    @NonNull
    @Override
    public BookBorrorAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.borror_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final BookBorrorAdapter.ViewHolder holder, int position) {
        final BorrorBookList bookList = blist.get(position);
        holder.tvBname.setText(bookList.getBbname());
        holder.tvBbtime.setText(bookList.getBbtime());
        holder.tvBdtime.setText(bookList.getBdtime());
        holder.btnBreturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                blist.remove(bookList);
                BookBorrorAdapter.this.notifyDataSetChanged();
                delBorrorBook(bookList);
                updateNums1(bookList);
            }
        });
    }

    private void updateNums1(BorrorBookList bookList) {
        String bbc = bookList.getBbname();
        Log.e("还书",bbc);
        BmobQuery<Book> bmobQuery = new BmobQuery<Book>();
        bmobQuery.addWhereEqualTo("name", bbc);
        bmobQuery.findObjects(new FindListener<Book>() {
            @Override
            public void done(List<Book> list, BmobException e) {
                if (e == null) {
                    Log.e("*-*", list.get(0).getName()+"-"+list.get(0).getObjectId());
                    updateNums(list.get(0).getObjectId(),list.get(0).getBookNums());
                } else {
                    Log.e("err", e.toString());
                }
            }
        });
    }

    private void updateNums(String objectId, int bookNums) {
        Book gameScore = new Book();
        int bos = bookNums + 1;
        gameScore.setBookNums(bos);
        gameScore.update(objectId, new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if(e==null){
                    Log.e("++++++++1111","更新成功");
                }else{
                    Log.e("+++++++1111b","更新失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }

    private void delBorrorBook(BorrorBookList bookList) {
        String delbook =null;
        String deltime =null;
        if (bookList.getI()==1111){
            delbook = "SBook1";
            deltime = "Time1";
        }else if (bookList.getI()==2222){
            delbook = "SBook2";
            deltime = "Time2";
        }else if (bookList.getI()==3333){
            delbook = "SBook3";
            deltime = "Time3";
        }
        String bbid = bookList.getBbid();
        Log.e("还书",bbid);
        BorrorBook gameScore = new BorrorBook();
        gameScore.remove(delbook);
        gameScore.remove(deltime);
        gameScore.update(bbid,new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Log.e("bmob","删除成功");
                }else{
                    Log.e("bmob",e.toString());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return blist.size();
    }

}
